package com.jason.happy.common.framework;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;

import java.lang.ref.WeakReference;

/**
 * Created by hacker on 17-6-25.
 */

public abstract class BaseLogicPresenter<T extends BaseLogicView> {

    private WeakReference<T> mViewRef;

    public void attachView(T view) {
        mViewRef = new WeakReference(view);
    }

    public T getLogicView() {
        return mViewRef.get();
    }

    public boolean isAttachView() {
        return mViewRef != null && mViewRef.get() != null;
    }

    public void removeView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }


    /**
     * @param toActivity
     * @param view
     */
    public void startActivityByScale(Class<? extends Activity> toActivity, View view) {
        ActivityOptionsCompat compat = ActivityOptionsCompat.makeScaleUpAnimation(view,
                view.getWidth() / 2, view.getHeight() / 2, 0, 0);
        Activity activity = getLogicView().getActivity();
        ActivityCompat.startActivity(activity, new Intent(activity, toActivity),
                compat.toBundle());
    }

}
